﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Medical.EntityType;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using Sector7.Medical.Biz;
using Sector7.Medical.Consts;
using Sector7.Framework.Utility;
using Sector7.Medical.Utility;

namespace Sector7.Medical.STOM.Controls
{
    /// <summary>
    /// DispenseDrugControl.xaml 的交互逻辑
    /// </summary>
    public partial class DispenseDrugControlForClinic : UserControl
    {
        #region Accessor
        #endregion

        #region Public functions
        #endregion

        #region Private functions

        /// <summary>
        /// 病人加载完成事件
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="e"></param>
        private void ReadCardControl_PatientLoaded(object obj, Events.PatientLoadedEventArgs e)
        {
            ResetControl(false);

            if (e.IsSuccess)
            {
                M7_Patient patient = e.Patient;
                LoadPatientInfor(patient);
            }
        }

        /// <summary>
        /// Load Patient
        /// </summary>
        /// <param name="patient"></param>
        private void LoadPatientInfor(M7_Patient patient)
        {
            patientCard.LoadPatient(patient);

            string citizenId = patient.CitizenId;

            m_medicalRecordList = Agent_BIZS.GetMedicalRecordByCitizenAndStatus(citizenId, C_BillStatus.NEW);
            if (m_medicalRecordList == null || m_medicalRecordList.Count == 0)
            {
                Alert.ShowDialog(AlertType.Error, "当前病人无相应处方记录");
            }
            else
            {
                m_recipeList = new List<M7_Recipe>();

                foreach (M7_MedicalRecord mr in m_medicalRecordList)
                {
                    List<M7_Recipe> recipeList = Agent_BIZS.GetPatientRecipeListByEmrIdAndStatus(mr.EmrId, C_BillStatus.PAYMENTED);
                    if (recipeList != null && recipeList.Count != 0)
                    {
                        m_recipeList.AddRange(recipeList.ToArray());
                    }
                }
                
                if (m_recipeList == null || m_recipeList.Count == 0)
                {
                    Alert.ShowDialog(AlertType.Error, "当前病人无相应处方记录");
                }
                else
                {
                    List<M7_RecipeItem> itemList = new List<M7_RecipeItem>();
                    foreach (M7_Recipe recipe in m_recipeList)
                    {
                        itemList.AddRange(recipe.Items.ToArray());
                    }

                    ClientUtil.LoadItemsSource(dtgMain, itemList);
                    dtgMain.Items.Refresh();

                    string nowDTStr = DateTime.Now.ToString("yyyyMMdd"); ;
                    int billSeed = Agent_SYSS.GetTernarySeed(C_M7_TableName.DISPENSE_BILL, nowDTStr, C_BillType.DISPENSE_RECORD.ToString());
                    long billId = Convert.ToInt64(PrimaryKeyUtil.GetPrimaryKey(C_M7_TableName.DISPENSE_BILL, nowDTStr, C_BillType.DISPENSE_RECORD, billSeed));

                    txbBillId.Text = billId.ToString();
                    txbDeptName.Text = "药房一";//Senvironments.CurrentAccount is M7_Employee ? (Senvironments.CurrentAccount as M7_Employee).Department.DeptName : "";
                    txbOperationName.Text = Senvironments.CurrentAccount is M7_Employee ? (Senvironments.CurrentAccount as M7_Employee).FullName : "";
                }
            }
        }

        /// <summary>
        /// Cancel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult mbr = Alert.ShowDialog(AlertType.Question, "取消操作后,将清除窗口包含信息,是否继续？");
            if (mbr == MessageBoxResult.Yes)
            {
                ResetControl(true);
            }
        }

        /// <summary>
        /// Reset Control
        /// </summary>
        public void ResetControl(bool resetReadCardControl)
        {
            if (resetReadCardControl)
            {
                ReadCardControl.ResetReadCardControl();
            }
            patientCard.ResetPatientCard();
            txbBillId.Text = "";
            txbDeptName.Text = "";
            txbOperationName.Text = "";
            txbAgentName.Text = "";
            ClientUtil.UnLoadItemsSource(dtgMain);
            dtgMain.Items.Refresh();
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            if (m_recipeList != null && m_recipeList.Count > 0)
            {
                M7_DispenseBill bill = new M7_DispenseBill();
                bill.BillId = Convert.ToInt64(txbBillId.Text.Trim());
                bill.EmrId = m_recipeList[0].EmrId;
                bill.DeptId = 0;
                bill.OperatorId = Senvironments.CurrentAccount is M7_Employee ? (Senvironments.CurrentAccount as M7_Employee).EmpId.ToString() : "";
                bill.AgentName = txbAgentName.Text.Trim();
                bill.CreateTime = DateTime.Now;
                bill.Status = C_BillStatus.COMPLETED;
                bill.RecipeList = m_recipeList;
                bill.MedicalRecordList = m_medicalRecordList;

                AddDispenseBill(bill);

                Alert.ShowDialog(AlertType.Notification, "发药成功");
                ResetControl(true);
            }
            else
            {
                Alert.ShowDialog(AlertType.Error, "当前病人无相应处方记录");
            }
        }

        /// <summary>
        /// Add Dispense Bill
        /// </summary>
        /// <param name="dispenseBill"></param>
        private int AddDispenseBill(M7_DispenseBill dispenseBill)
        {
            int ret = Agent_STOS.AddDispenseBill(dispenseBill);
            return ret;
        }

        #endregion

        #region Structure
        
        /// <summary>
        /// 构造方法
        /// </summary>
        public DispenseDrugControlForClinic()
        {
            InitializeComponent();
        }

        #endregion

        #region Inner members

        /// <summary>
        /// 处方信息列表
        /// </summary>
        List<M7_Recipe> m_recipeList = new List<M7_Recipe>();

        /// <summary>
        /// 电子病历记录
        /// </summary>
        List<M7_MedicalRecord> m_medicalRecordList = new List<M7_MedicalRecord>();
        #endregion
    }
}
